<!DOCTYPE html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <!-- Bootstrap CSS -->
        <link
            rel="stylesheet"
            href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha2/css/bootstrap.min.css"
            integrity="sha384-DhY6onE6f3zzKbjUPRc2hOzGAdEf4/Dz+WJwBvEYL/lkkIsI3ihufq9hk9K4lVoK"
            crossorigin="anonymous"
        />

        <title>Hello, world!</title>
    </head>
    <body>
        <!-- margin bottom -->
        <!-- container 布局容器，提供 12 栅格 -->
        <div class="container">
            <div class="w-50 mx-auto mt-5">
                <div class="mb-3">
                    <label for="username" class="form-label">用户名</label>

                    <input
                        class="form-control"
                        id="username"
                        placeholder="请输入用户名"
                    />

                    <!-- is-invalid 验证输入框样式 -->
                    <!-- valid-feedback 验证成功 -->
                    <p id="usernameError"></p>
                </div>

                <div class="mb-3">
                    <label for="password" class="form-label">密码</label>

                    <input
                        class="form-control"
                        id="password"
                        placeholder="请输入密码"
                    />

                    <!-- is-invalid 验证输入框样式 -->
                    <!-- valid-feedback 验证成功 -->
                    <p></p>
                </div>

                <button class="btn btn-primary btn-block">登录</button>
            </div>
        </div>

        <script>
            // 获取 DOM 元素
            const usernameInput = document.getElementById('username');
            const usernameP = document.getElementById('usernameError');
            const submitBtn = document.querySelector('.btn');
            //
            usernameInput.addEventListener('blur', () => {
                // 1. 创建 ajax 对象
                const xhr = new XMLHttpRequest();

                // 2. 建立链接
                xhr.open('GET', `/users?username=${usernameInput.value}`);

                // 只接收 json 数据
                xhr.responseType = 'json';

                // 3. 监听服务器响应
                xhr.onreadystatechange = function () {
                    if (xhr.readyState !== 4) {
                        return;
                    }

                    // 读取数据
                    // console.log(xhr.response);
                    const res = xhr.response;
                    if (res.code !== 200) {
                        usernameInput.classList.add('is-invalid');
                        usernameInput.classList.remove('is-valid');
                        usernameP.classList.add('invalid-feedback');
                        usernameP.classList.remove('valid-feedback');
                        usernameP.innerHTML = res.message;
                    } else {
                        usernameInput.classList.add('is-valid');
                        usernameInput.classList.remove('is-invalid');
                        usernameP.classList.add('valid-feedback');
                        usernameP.classList.remove('invalid-feedback');
                        usernameP.innerHTML = res.message;
                    }
                };

                // 4. 发送请求
                xhr.send();
            });
        </script>
    </body>
</html>
